const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const api = require("./router/router");
const path = require("path");
const server = express();

server.use((req,res,next)=>{
    res.cc = function(err,code=500){
        res.send({
            code,
            msg:err instanceof Error? err.message:err,
            val:[]
        })
    }
    res.s =function (msg,val,code=500){
        res.send({code,msg,val})
    }
    next()
})
server.use(bodyParser.urlencoded({extended:false}))
server.use(cors());
server.use(api);
server.use(express.static(path.join(__dirname,"./public")))
server.use(express.static(path.join(__dirname,"./view")))
// 挂载错误中间件
server.use((err,req,res,next)=>{
    if(err){
        res.cc(err);
    }
})
server.listen(5122,()=>{
    console.log("server running at 5122");
})